package com.badlogic.agdwikisamples;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

import com.badlogic.agdwikisamples.utils.BufferUtils;

public class VBOUpdateBug extends Activity implements GLSurfaceView.Renderer {
	
	int vboHandle;
	int iboHandle;
	ShortBuffer indices;
	FloatBuffer vertices1;
	FloatBuffer vertices2;
	GLSurfaceView glView;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		
		glView = new GLSurfaceView(this);
		glView.setRenderer(this);
		setContentView(glView);
	}
	
	public void onPause() {
		super.onPause();
		glView.onPause();
	}
	
	public void onResume() {
		super.onResume();
		glView.onResume();
	}
	
	@Override
	public void onDrawFrame(GL10 gl10) {
		GL11 gl = (GL11)gl10;
		gl.glClearColor(1, 0, 0, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);		
		gl.glViewport(0, 0, glView.getWidth(), glView.getHeight());		
		
		// bind vbo and ibo and enable vertex array client state
		gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, vboHandle);
		gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, iboHandle);		
		gl.glEnableClientState(GL11.GL_VERTEX_ARRAY);
		gl.glVertexPointer(2, GL11.GL_FLOAT, 5 * 4, 0);
										
		// upload data of vertices1 to vbo. contains 2 rectangles.
		gl.glBufferData(GL11.GL_ARRAY_BUFFER, vertices1.limit() * 4, vertices1, GL11.GL_DYNAMIC_DRAW);									
		gl.glDrawElements(GL11.GL_TRIANGLES, 12, GL11.GL_UNSIGNED_SHORT, 0);
				
		// upload data of vertices2 to vbo, contains 1 rectangle.
		gl.glBufferData(GL11.GL_ARRAY_BUFFER, vertices2.limit() * 4, vertices2, GL11.GL_DYNAMIC_DRAW);		
		gl.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_SHORT, 0);
		
		// unbind buffers
		gl.glBindBuffer(GL11.GL_ARRAY_BUFFER, 0);
		gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);	
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		
	}

	@Override
	public void onSurfaceCreated(GL10 gl10, EGLConfig config) {
		GL11 gl = (GL11)gl10; 
		
		// generate vbo and ibo handles
		IntBuffer handleBuf = BufferUtils.newIntBuffer(2);		
		gl.glGenBuffers(2, handleBuf);
		vboHandle = handleBuf.get(0);
		iboHandle = handleBuf.get(1);
	
		// create indices (2 rectangles, each consisting of 2 triangles indexing 4 vertices each		
		indices = BufferUtils.newShortBuffer(12);
		indices.put(new short[] { 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4 });
		indices.flip();
		
		// upload indices once
		gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, iboHandle);
		gl.glBufferData(GL11.GL_ELEMENT_ARRAY_BUFFER, indices.limit() * 2, indices, GL11.GL_DYNAMIC_DRAW);
		gl.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, 0);
		
		// create vertices, vertices1 holds 2 rectangles having 4 vertices each. vertex attributes
		// are position (2d), color (packed 32-bit argb), tex coords (uv) 
		float c = Float.intBitsToFloat(0xffffffff);
		vertices1 = BufferUtils.newFloatBuffer(40);
		vertices1.put(new float[] {-1, -1, c, 0, 0, 
							   	  -1, -0.5f, c, 0, 1,
							   	  -0.5f, -0.5f, c, 1, 1,
							   	  -0.5f, -1f, c, 1, 0,
							   	  0, -1, c, 0, 0,
							   	  0, -0.5f, c, 0, 1,
							   	  0.5f, -0.5f, c, 1, 1,
							   	  0.5f, -1f, c, 1, 0 });
		vertices1.flip();
		
		// create second set of vertices, vertices2 holds 1 rectangle having 4 vertices. vertex attributes
		// are position (2d), color (packed 32-bit argb), tex coords (uv) 		
		vertices2 = BufferUtils.newFloatBuffer(20);
		vertices2.put(new float[] {-1, 0, c, 0, 0, 
								   -1, 0.5f, c, 0, 1,
								   -0.5f, 0.5f, c, 1, 1,
								   -0.5f, 0, c, 1, 0 });
		vertices2.flip();
	}
}
